from ogreEx.axis import swap, swapAbs
from ogreEx.blender.FNode import get_parent_matrix

## Ogre node helper
def _ogre_node_helper(doc, ob, objects, prefix='', pos=None, rot=None, scl=None):
    # shouldn't this be matrix_local?
    mat = get_parent_matrix(ob, objects).inverted() * ob.matrix_world

    tagNode = doc.createElement('node')
    tagNode.setAttribute('name',prefix+ob.name)
    if ob.parent_bone!='':
        tagNode.setAttribute('bone',ob.parent_bone)
    tagPosition = doc.createElement('position' )
    tagRotation = doc.createElement('rotation' )       #('quaternion')
    tagScale    = doc.createElement('scale'    )

    #for n in (tagPosition,tagRotation,tagScale, tagTest): tagNode.appendChild(n)
    for n in (tagPosition,tagRotation,tagScale):
        tagNode.appendChild(n)

    if pos: v = swap(pos)
    #else: v = swap(ob.location)
    else: v = swap( mat.to_translation() )

    tagPosition.setAttribute('x', '%6f'%v.x)
    tagPosition.setAttribute('y', '%6f'%v.y)
    tagPosition.setAttribute('z', '%6f'%v.z)

    #ri = mat.to_quaternion().inverted().to_matrix()
    #scale = ri.to_4x4() * mat
    scale = swapAbs(ob.scale)

    if scl:        # this should not be used
        v = swapAbs(scl)
        x=v.x; y=v.y; z=v.z
        tagScale.setAttribute('x', '%6f'%x)
        tagScale.setAttribute('y', '%6f'%y)
        tagScale.setAttribute('z', '%6f'%z)
    else:        # scale is different in Ogre from blender - rotation is removed
        #v = swapAbs( scale.to_scale() )
        v = scale
        x=v.x; y=v.y; z=v.z
        tagScale.setAttribute('x', '%6f'%x)
        tagScale.setAttribute('y', '%6f'%y)
        tagScale.setAttribute('z', '%6f'%z)

    #quaternion = mat;
    #quaternion = quaternion.Scale(0, 4, (-1,-1,-1))
    #quaternion = mat.Scale(0, 4, (1,1,1))
    rotation_mode=ob.rotation_mode;
    ob.rotation_mode='QUATERNION'
    quaternion = swap(ob.rotation_quaternion);
    ob.rotation_mode=rotation_mode;

    #if rot: v = swap(rot)
    #else: v = swap( quaternion.to_quaternion() )
    v = quaternion
    tagRotation.setAttribute('qx', '%6f'%v.x)
    tagRotation.setAttribute('qy', '%6f'%v.y)
    tagRotation.setAttribute('qz', '%6f'%v.z)
    tagRotation.setAttribute('qw', '%6f'%v.w)

    return tagNode


def _mesh_entity_helper( doc, ob, o ):
    ## extended format - BGE Physics ##
    o.setAttribute('mass'         ,str(ob.game.mass            ))
    o.setAttribute('mass_radius'  ,str(ob.game.radius          ))
    o.setAttribute('physics_type' ,    ob.game.physics_type     )
    o.setAttribute('actor'        ,str(ob.game.use_actor       ))
    o.setAttribute('ghost'        ,str(ob.game.use_ghost       ))
    o.setAttribute('velocity_min' ,str(ob.game.velocity_min    ))
    o.setAttribute('velocity_max' ,str(ob.game.velocity_max    ))

    o.setAttribute('lock_trans_x' ,str(ob.game.lock_location_x ))
    o.setAttribute('lock_trans_y' ,str(ob.game.lock_location_y ))
    o.setAttribute('lock_trans_z' ,str(ob.game.lock_location_z ))

    o.setAttribute('lock_rot_x'   ,str(ob.game.lock_rotation_x ))
    o.setAttribute('lock_rot_y'   ,str(ob.game.lock_rotation_y ))
    o.setAttribute('lock_rot_z'   ,str(ob.game.lock_rotation_z ))

    o.setAttribute('anisotropic_friction', str(ob.game.use_anisotropic_friction))
    x,y,z = ob.game.friction_coefficients
    o.setAttribute('friction_x' ,str(x))
    o.setAttribute('friction_y' ,str(y))
    o.setAttribute('friction_z' ,str(z))

    o.setAttribute('damping_trans'  ,str(ob.game.damping          ))
    o.setAttribute('damping_rot'    ,str(ob.game.rotation_damping ))

    o.setAttribute('inertia_tensor' ,str(ob.game.form_factor      ))

    mesh = ob.data
    # custom user props
    for prop in mesh.items():
        propname, propvalue = prop
        if not propname.startswith('_'):
            user = doc.createElement('user_data')
            o.appendChild( user )
            user.setAttribute('name'  ,      propname            )
            user.setAttribute('value' ,str  (propvalue)          )
            user.setAttribute('type'  ,type (propvalue).__name__ )
